import threading

import pygame.sprite
from numba import njit
from pygame.locals import *

import globals

from entity.background import Background
from entity.bullet import *
from entity.enemy import *
from entity.myplane import MyPlane
from entity.supply import *
from mapper.score_mapper import Score_mapper
from mapper.log_mapper import Log_Mapper


# @njit
def inc_speed(target, inc):
    for e in target:
        e.speed += inc


# 向数据库保存分数
def save_score(user_name, score):
    score_mapper:Score_mapper = Score_mapper("./db/data.db")
    log_mapper:Log_Mapper = Log_Mapper("./db/data.db")
    score_mapper.save_score(user_name, score)
    log_mapper.save_info_log(user_name,"SaveScore")



def resder_config():
    conf = configparser.ConfigParser()
    curpath = os.path.dirname(os.path.realpath(__file__))
    path = os.path.join(curpath, 'config.ini')
    conf.read(path, encoding="utf-8")
    client_config = conf["client"]
    gameplay_config = conf["gameplay"]
    return client_config, gameplay_config


"""游戏程序入口"""


def start_game():
    global invincible_image, invincible_image_rect
    client_config, gameplay_config = resder_config()
    pygame.init()
    width, height = int(client_config["bg_width"]), int(client_config["bg_height"])
    bg_size = width, height
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption(client_config["title"])

    # ------------------------加载静态资源--------------------------

    # 音频
    pygame.mixer.music.load("./sound/background.mp3")
    pygame.mixer.music.set_volume(0.6)

    bullet_sound = pygame.mixer.Sound("./sound/bullet.wav")
    bullet_sound.set_volume(0.1)

    supply_sound = pygame.mixer.Sound("./sound/supply.wav")
    supply_sound.set_volume(0.2)

    bomb_sound = pygame.mixer.Sound("./sound/use_bomb.wav")
    bomb_sound.set_volume(0.5)

    get_bullet_sound = pygame.mixer.Sound("./sound/get_bullet.wav")
    get_bullet_sound.set_volume(0.2)

    get_bomb_sound = pygame.mixer.Sound("./sound/get_bomb.wav")
    get_bomb_sound.set_volume(0.2)

    upgrade_sound = pygame.mixer.Sound("./sound/upgrade.wav")
    upgrade_sound.set_volume(0.2)

    enemy3_fly_sound = pygame.mixer.Sound("./sound/enemy3_flying.wav")
    enemy3_fly_sound.set_volume(0.2)

    enemy3_down_sound = pygame.mixer.Sound("./sound/enemy3_down.wav")
    enemy3_down_sound.set_volume(0.2)

    enemy2_down_sound = pygame.mixer.Sound("./sound/enemy2_down.wav")
    enemy2_down_sound.set_volume(0.2)

    enemy1_down_sound = pygame.mixer.Sound("./sound/enemy1_down.wav")
    enemy1_down_sound.set_volume(0.2)

    me_down_sound = pygame.mixer.Sound("./sound/me_down.wav")
    me_down_sound.set_volume(0.2)

    game_over_sound = pygame.mixer.Sound("./sound/game_over.mp3")
    game_over_sound.set_volume(0.7)

    # 图
    life_image = pygame.image.load("./images/life.png")
    bomb_image = pygame.image.load("./images/bomb.png")
    paused_nor_image = pygame.image.load("./images/pause_nor.png")
    paused_pressed_image = pygame.image.load("./images/pause_pressed.png")
    resume_nor_image = pygame.image.load("./images/resume_nor.png")
    resume_pressed_image = pygame.image.load("./images/resume_pressed.png")
    title_image = pygame.image.load("./images/title.png").convert_alpha()
    begin_image = pygame.image.load("./images/begin.png").convert_alpha()
    over_image = pygame.image.load("./images/game_over.png")
    again_image = pygame.image.load("./images/again.png")

    # 字体
    game_begin_font = pygame.font.Font("./font/PortableTube-1.ttf", 15)
    game_over_font = pygame.font.Font("./font/DS-DIGIB-2.ttf", 48)
    score_font = pygame.font.Font('./font/DS-DIGIB-2.ttf', 36)
    bomb_font = pygame.font.Font("./font/DS-DIGI-1.ttf", 48)

    # ---------------------部分颜色常量-------------------------
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)

    # ----------------------游戏内容相关-------------------------
    bg1 = Background()
    bg2 = Background(True)
    bg_group = pygame.sprite.Group(bg1, bg2)

    # 循环播放背景音乐
    pygame.mixer.music.play(-1)

    # ----------------------初始化游戏事件-------------------------
    SUPPLY_TIME = pygame.USEREVENT
    DOUBLE_BULLET_TIME = pygame.USEREVENT + 1
    INVINCIBLE_TIME = pygame.USEREVENT + 2

    # 暂停事件
    paused_image = paused_nor_image
    paused_rect = paused_nor_image.get_rect()
    paused_rect.left = width - paused_rect.width - 10
    paused_rect.top = 10

    # ----------------------游戏行为相关设置-------------------------
    # 子弹数量
    BULLET1_NUM = int(gameplay_config["BULLET1_NUM"])
    BULLET2_NUM = int(gameplay_config["BULLET2_NUM"])

    # 设置生命数量
    life_rect = life_image.get_rect()
    life_num = int(gameplay_config['life_num'])

    # 统计得分
    score: int = 0

    # 设置初始游戏等级
    level: int = int(gameplay_config['level'])

    # 是否使用超级子弹
    is_double_bullet = gameplay_config["is_double_bullet"] == "1"

    # 设置时钟来控制屏幕刷新频率
    clock = pygame.time.Clock()

    # 游戏运行控制位
    running: bool = True

    # 游戏暂停控制位
    paused: bool = False

    # 设置图像转换变量
    switch_image: bool = False

    # 延迟
    delay: int = 100

    # 无敌模式控制位
    invincible: bool = False

    # 战机升级控制位
    super_me: bool = True
    super_bullet: bool = False

    # 是否退出选择页面
    out: bool = False

    # --------------------实例化游戏对象------------------------#

    # 实例化我方战机
    me = MyPlane(bg_size)

    # 生成敌方精灵组, 检测时只需将精灵组与me比较
    enemy_group = pygame.sprite.Group()

    # 生成小型敌机组
    small_enemies = pygame.sprite.Group()
    SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 50)

    # 生成中型敌机组
    mid_enemies = pygame.sprite.Group()
    MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 6)

    # 生成大型敌机组
    big_enemies = pygame.sprite.Group()
    BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 4)

    # 生成普通子弹
    bullet1 = []
    bullet1_index = 0
    for i in range(BULLET1_NUM):
        bullet1.append(Bullet1((me.rect.centerx - 20, me.rect.centery)))

    # 生成超级子弹
    bullet2 = []
    bullet2_index = 0
    for i in range(BULLET2_NUM // 3):
        bullet2.append(Bullet2((me.rect.centerx - 30, me.rect.centery)))
        bullet2.append(Bullet2((me.rect.centerx, me.rect.centery)))
        bullet2.append(Bullet2((me.rect.centerx + 30, me.rect.centery)))

    # 设置全屏炸弹
    bomb_image_rect = bomb_image.get_rect()

    # 补给包
    bullet_supply = BulletSupply(bg_size)
    bomb_supply = BombSupply(bg_size)
    pygame.time.set_timer(SUPPLY_TIME, int(gameplay_config["bullet_supply_time"]) * 1000)

    # 中弹图片索引初始化
    enemy1_destroy_index = 0
    enemy2_destroy_index = 0
    enemy3_destroy_index = 0
    me_destroy_index = 0

    # 用于阻止重复访问临时文件
    recorded = False

    # ---------------------------------最后准备-----------------------------------------

    # 初始化游戏开始画面
    if int(gameplay_config["developer_model"]) != 0:
        invincible_image = pygame.image.load("./images/invincible.png").convert_alpha()
    pygame_logo = pygame.image.load("./images/pygame_logo.png").convert_alpha()
    mediapipe_logo = pygame.image.load("./images/mediapipe_logo.png").convert_alpha()
    title_image_rect = title_image.get_rect()
    begin_image_rect = begin_image.get_rect()
    if int(gameplay_config["developer_model"]) != 0:
        invincible_image_rect = invincible_image.get_rect()
    mediapipe_logo_rect = mediapipe_logo.get_rect()
    pygame_logo_rect = pygame_logo.get_rect()

    # 游戏结束画面
    again_image_rect = again_image.get_rect()
    over_image_rect = over_image.get_rect()

    # 绘制背景图像
    bg_group.update()
    bg_group.draw(screen)

    # ------------------------------------RUN---------------------------------------
    while True:
        bg_group.update()
        bg_group.draw(screen)
        # 绘制开始页面元素
        title_image_rect.left = (width - title_image_rect.width) // 2
        title_image_rect.top = 0
        begin_image_rect.left = (width - begin_image_rect.width) // 2
        begin_image_rect.top = (height - begin_image_rect.height) // 2 + 150
        if int(gameplay_config["developer_model"]) != 0:
            invincible_image_rect.left = (width - invincible_image_rect.width) // 2
            invincible_image_rect.top = (height - invincible_image_rect.height) // 2 + 200
        pygame_logo_rect.left = (width - pygame_logo_rect.width) // 2 - 50
        pygame_logo_rect.top = height - pygame_logo_rect.height - 2
        mediapipe_logo_rect.left = (width - mediapipe_logo_rect.width) // 2 + 50
        mediapipe_logo_rect.top = height - mediapipe_logo_rect.height
        screen.blit(title_image, title_image_rect)
        screen.blit(begin_image, begin_image_rect)
        if int(gameplay_config["developer_model"]) != 0:
            screen.blit(invincible_image, invincible_image_rect)
        screen.blit(pygame_logo, pygame_logo_rect)
        screen.blit(mediapipe_logo, mediapipe_logo_rect)

        version_text = game_begin_font.render(client_config["version_info"], True, WHITE)
        screen.blit(version_text, ((width - version_text.get_width()) // 2, (height - version_text.get_height()) - 40))
        bgm_text = game_begin_font.render("BGM:<<GUNNAC>>-Track4", True, WHITE)
        screen.blit(bgm_text, ((width - bgm_text.get_width()) // 2, (height - bgm_text.get_height()) - 60))
        clock.tick(60)
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    # 正常模式
                    if begin_image_rect.left < event.pos[0] < begin_image_rect.right and \
                            begin_image_rect.top < event.pos[1] < begin_image_rect.bottom:
                        # 重置结束音乐
                        game_over_sound_pylyed = False
                        invincible = False
                        out = True
                        # 生成补给包
                        pygame.time.set_timer(SUPPLY_TIME, int(gameplay_config["bullet_supply_time"]) * 1000)
                        break
                    # 开发者模式，此模式下战机无敌，补给5秒一个,无限炸药包
                    if invincible_image_rect.left < event.pos[0] < invincible_image_rect.right and \
                            invincible_image_rect.top < event.pos[1] < invincible_image_rect.bottom:
                        # 重置结束音乐
                        game_over_sound_pylyed = False
                        invincible = True
                        globals.set_invincible(True)
                        out = True
                        pygame.time.set_timer(SUPPLY_TIME, 5 * 1000)
                        life_num = 1
                        break
        if out:
            break

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1 and paused_rect.collidepoint(event.pos):
                    paused = not paused
                    if paused:
                        pygame.time.set_timer(SUPPLY_TIME, 0)
                        pygame.mixer.music.pause()
                        pygame.mixer.pause()
                    else:
                        pygame.time.set_timer(SUPPLY_TIME, 30 * 1000)
                        pygame.mixer.music.unpause()
                        pygame.mixer.unpause()

            elif event.type == MOUSEMOTION:
                if paused_rect.collidepoint(event.pos):
                    if paused:
                        paused_image = resume_pressed_image
                    else:
                        paused_image = paused_pressed_image
                else:
                    if paused:
                        paused_image = resume_nor_image
                    else:
                        paused_image = paused_nor_image
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if globals.get_bomb_num() > 0:
                        bomb_sound.play()
                        if not invincible:
                            globals.use_bomb()
                        for e in enemy_group:
                            if e.rect.bottom > 0:
                                e.active = False
            elif event.type == SUPPLY_TIME:
                supply_sound.play()
                if choice([True, False]):
                    bomb_supply.reset()
                else:
                    bullet_supply.reset()

            elif event.type == DOUBLE_BULLET_TIME:
                is_double_bullet = False
                pygame.time.set_timer(DOUBLE_BULLET_TIME, 0)

            elif event.type == INVINCIBLE_TIME:
                me.invincible = False
                pygame.time.set_timer(INVINCIBLE_TIME, 0)

        # -----------------------------难度随着等级提升--------------------------------#
        if level == 1 and score > 180000:
            level = 2
            upgrade_sound.play()
            SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
            MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 2)
            BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
            inc_speed(small_enemies, 1)

        if level == 2 and score > 360000:
            level = 3
            upgrade_sound.play()
            SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
            MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
            BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
            inc_speed(small_enemies, 0.5)
            inc_speed(mid_enemies, 0.5)

        # 战机升级， 子弹四倍伤害，移动速度提升二倍
        if level == 3 and score > 800000 and super_me:
            level = 4
            upgrade_sound.play()
            super_me = False
            super_bullet = True
            upgrade_sound.play()
            # me.change_face(bg_size)
            SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 5)
            MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
            BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
            inc_speed(small_enemies, 0.5)
            inc_speed(mid_enemies, 0.5)

        if level == 4 and score > 1200000:
            level = 5
            upgrade_sound.play()
            SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
            MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 2)
            BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
            # 提升小型敌机速度
            inc_speed(small_enemies, 0.1)
            # 提升中型敌机速度
            inc_speed(mid_enemies, 0.1)
            # 提升大型敌机速度
            inc_speed(big_enemies, 0.5)

        if level == 5 and score > 2000000:
            level = 6
            upgrade_sound.play()
            SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
            MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
            BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
            # 提升小型敌机速度
            inc_speed(small_enemies, 0.1)
            # 提升中型敌机速度
            inc_speed(mid_enemies, 0.1)
            # 提升大型敌机速度
            inc_speed(big_enemies, 0.1)

        if level == 6 and score > 3000000:
            level = 7
            upgrade_sound.play()
            SmallEnemy.add_small_enemies(small_enemies, enemy_group, bg_size, 1)
            MidEnemy.add_mid_enemies(mid_enemies, enemy_group, bg_size, 1)
            BigEnemy.add_big_enemies(big_enemies, enemy_group, bg_size, 1)
            # 提升小型敌机速度
            inc_speed(small_enemies, 0.1)
            # 提升中型敌机速度
            inc_speed(mid_enemies, 0.1)
            # 提升大型敌机速度
            inc_speed(big_enemies, 0.1)

        # 绘制背景图像
        bg_group.update()
        bg_group.draw(screen)

        # 没有暂停并且战机生命大于0
        if life_num and not paused:
            # 检测用户键盘是否按下
            key_pressed = pygame.key.get_pressed()
            if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
                me.move_up()
            if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
                me.move_down()
            if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
                me.move_left()
            if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
                me.move_right()
            # 手势控制enemy_group
            threading.Thread(target=me.move_by_finger(bg_size, enemy_group, bomb_sound))

            # 绘制全屏炸弹
            if bomb_supply.active:
                bomb_supply.move()
                screen.blit(bomb_supply.image, bomb_supply.rect)
                if pygame.sprite.collide_mask(bomb_supply, me):
                    get_bomb_sound.play()
                    if globals.get_bomb_num() < 5:
                        globals.add_bomb()
                    bomb_supply.active = False

            # 绘制超级子弹
            if bullet_supply.active:
                bullet_supply.move()
                screen.blit(bullet_supply.image, bullet_supply.rect)
                # 检测战机是否获得超级子弹
                if pygame.sprite.collide_mask(bullet_supply, me):
                    get_bullet_sound.play()
                    is_double_bullet = True
                    pygame.time.set_timer(DOUBLE_BULLET_TIME, 18 * 1000)
                    bullet_supply.active = False

            # 发射子弹
            if not (delay % 10):
                bullet_sound.play()
                if is_double_bullet:
                    bullets = bullet2
                    bullets[bullet2_index].reset((me.rect.centerx - 20, me.rect.centery))
                    bullets[bullet2_index + 1].reset((me.rect.centerx - 60, me.rect.centery))
                    bullets[bullet2_index + 2].reset((me.rect.centerx + 20, me.rect.centery))
                    bullet2_index = (bullet2_index + 3) % BULLET2_NUM
                else:
                    bullets = bullet1
                    bullets[bullet1_index].reset((me.rect.centerx - 4, me.rect.centery))
                    bullet1_index = (bullet1_index + 1) % BULLET1_NUM

            # 检测子弹是否与敌机相撞
            for b in bullets:
                if b.active:
                    b.move()
                    screen.blit(b.image, b.rect)
                    enemies_hit = pygame.sprite.spritecollide(b, enemy_group, False, pygame.sprite.collide_mask)
                    if enemies_hit:
                        b.active = False
                        for e in enemies_hit:
                            if e in mid_enemies or e in big_enemies:
                                if super_bullet:
                                    e.energy -= 4
                                else:
                                    e.energy -= 1
                                e.hit = True
                                if e.energy == 0:
                                    e.active = False
                            else:
                                e.active = False

            # 绘制小型敌机
            for each in small_enemies:
                if each.active:
                    each.move()
                    screen.blit(each.image, each.rect)
                else:
                    if not (delay % 3):
                        if enemy1_destroy_index == 0:
                            enemy1_down_sound.play()
                        screen.blit(each.destroy_images[enemy1_destroy_index], each.rect)
                        enemy1_destroy_index = (enemy1_destroy_index + 1) % 4
                        if enemy1_destroy_index == 0:
                            score += int(gameplay_config["s_enemy_score"])
                            each.reset()

            # 绘制中型敌机
            for each in mid_enemies:
                if each.active:
                    each.move()
                    if each.hit:  # 被打时触发打击特效
                        screen.blit(each.hit_image, each.rect)
                        each.hit = False
                    else:
                        screen.blit(each.image, each.rect)
                    # 绘制血槽
                    pygame.draw.line(screen,
                                     BLACK,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.right, each.rect.top - 5),
                                     2)
                    energy_remain = each.energy / MidEnemy.energy
                    if energy_remain > 0.3:
                        line_color = GREEN
                    else:
                        line_color = RED
                    pygame.draw.line(screen,
                                     line_color,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
                                     5)
                else:
                    if not (delay % 3):
                        if enemy2_destroy_index == 0:
                            enemy2_down_sound.play()
                        screen.blit(each.destroy_images[enemy2_destroy_index], each.rect)
                        enemy2_destroy_index = (enemy2_destroy_index + 1) % 4
                        if enemy2_destroy_index == 0:
                            score += int(gameplay_config["m_enemy_score"])
                            each.reset()

            # 绘制大型敌机
            for each in big_enemies:
                if each.active:
                    each.move()
                    if each.hit:  # 被打时触发打击特效
                        screen.blit(each.hit_image, each.rect)
                        each.hit = False
                    else:
                        if switch_image:
                            screen.blit(each.image1, each.rect)
                        else:
                            screen.blit(each.image2, each.rect)
                    # 绘制血槽
                    pygame.draw.line(screen,
                                     BLACK,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.right, each.rect.top - 5),
                                     5)
                    energy_remain = each.energy / BigEnemy.energy
                    if energy_remain > 0.4:
                        line_color = GREEN
                    else:
                        line_color = RED
                    pygame.draw.line(screen,
                                     line_color,
                                     (each.rect.left, each.rect.top - 5),
                                     (each.rect.left + each.rect.width * energy_remain, each.rect.top - 5),
                                     2)
                    if each.rect.bottom - 50 > 0:
                        enemy3_fly_sound.play()
                else:  # 毁灭
                    if not (delay % 3):
                        if enemy3_destroy_index == 0:
                            enemy3_down_sound.play()
                        screen.blit(each.destroy_images[enemy3_destroy_index], each.rect)
                        enemy3_destroy_index = (enemy3_destroy_index + 1) % 6
                        if enemy3_destroy_index == 0:
                            enemy3_fly_sound.stop()
                            score += int(gameplay_config["b_enemy_score"])
                            each.reset()

            # 检测我放战机是否与敌方战机相撞，相撞则返回列表
            enemies_down = pygame.sprite.spritecollide(me, enemy_group, False, pygame.sprite.collide_mask)
            if enemies_down and not me.invincible:
                if not invincible:
                    if score >= int(gameplay_config["damage_score"]): score -= int(gameplay_config["damage_score"])
                    me.active = False
                for e in enemies_down:
                    e.active = False

            # 绘制动态战机
            if me.active:
                if switch_image:
                    screen.blit(me.image1, me.rect)
                else:
                    screen.blit(me.image2, me.rect)
            else:
                if not (delay % 3):
                    if me_destroy_index == 0:
                        me_down_sound.play()
                    screen.blit(me.destroy_images[me_destroy_index], me.rect)
                    me_destroy_index = (me_destroy_index + 1) % 4
                    if me_destroy_index == 0:
                        life_num -= 1
                        me.reset()
                        pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000)

            # 绘制战机生命数量
            if life_num:
                for life in range(life_num):
                    screen.blit(life_image, (width - 10 - (life + 1) * life_rect.width, height - 10 - life_rect.height))

            # 绘制全屏炸弹
            bomb_text = bomb_font.render("x %d" % globals.get_bomb_num(), True, WHITE)
            text_rect = bomb_text.get_rect()
            screen.blit(bomb_image, (10, height - 12 - bomb_image_rect.height))
            screen.blit(bomb_text, (20 + bomb_image_rect.width, height - 10 - bomb_image_rect.height))

            # 绘制分数
            score_text = score_font.render("Score : %s" % str(score), True, WHITE)
            screen.blit(score_text, (10, 5))

        # 游戏结束画面
        elif life_num == 0:
            # 播放结束音效
            game_over_sound.play()

            # 停止全部音效
            pygame.mixer.stop()

            # 停止补给发放
            pygame.time.set_timer(SUPPLY_TIME, 0)
            if not recorded:
                recorded = True

                # 读取历史最高得分
                with open('record.ini', 'a+') as f:
                    result = f.read()
                    if result == "":
                        record_score = 0
                    else:
                        record_score = int(result)

                # 如果玩家得分高于最高分则存档
                if score > record_score:
                    user_name = globals.get_user_name()
                    save_score(user_name, score)

            # 绘制历史最高分
            record_score_text = score_font.render("Best : %d" % record_score, True, (255, 255, 255))
            screen.blit(record_score_text, (50, 50))

            # 绘制本次游戏分数
            game_over_text1 = game_over_font.render("Your Score", True, (255, 255, 255))
            game_over_text1_rect = game_over_text1.get_rect()
            game_over_text1_rect.left = (width - game_over_text1_rect.width) // 2
            game_over_text1_rect.top = height // 3
            screen.blit(game_over_text1, game_over_text1_rect)
            game_over_text2 = game_over_font.render(str(score), True, (255, 255, 255))
            game_over_text2_rect = game_over_text2.get_rect()
            game_over_text2_rect.left = (width - game_over_text2_rect.width) // 2
            game_over_text2_rect.top = game_over_text1_rect.top + 70
            screen.blit(game_over_text2, game_over_text2_rect)

            # 绘制重新开始
            again_image_rect.left = (width - again_image_rect.width) // 2
            again_image_rect.top = game_over_text2_rect.bottom + 50
            screen.blit(again_image, again_image_rect)

            # 绘制结束游戏
            over_image_rect.left = (width - over_image_rect.width) // 2
            over_image_rect.top = again_image_rect.bottom + 50
            screen.blit(over_image, over_image_rect)

            # 检测鼠标左键是否按下
            if pygame.mouse.get_pressed()[0]:
                pos = pygame.mouse.get_pos()
                if again_image_rect.left < pos[0] < again_image_rect.right and \
                        again_image_rect.top < pos[1] < again_image_rect.bottom:
                    # 重新开始游戏
                    start_game()
                if over_image_rect.left < pos[0] < over_image_rect.right and \
                        over_image_rect.top < pos[1] < over_image_rect.bottom:
                    # 退出游戏
                    pygame.quit()
            # pygame.quit()
            # # 结束背景音乐
            # if not game_over_sound_pylyed:
            #     pygame.mixer.music.stop()
            #     pygame.display.update()
            #     time.sleep(1)
            #     game_over_sound_pylyed = True
            # else:
            #     game_over_sound.stop()
            #     pygame.mixer.stop()
        # 绘制暂停图像
        screen.blit(paused_image, paused_rect)

        # 限制绘图时间
        if not (delay % 3):
            switch_image = not switch_image
        if delay:
            delay -= 1
        else:
            delay = 100

        # 刷新图像
        pygame.display.update()
        clock.tick(60)

# if __name__ == "__main__":
#     # try:
#     #     main()
#     # except SystemExit:
#     #     pass
#     # except:
#     #     traceback.print_exc()
#     main()
#     pygame.quit()
#     input()
